package com.lihepeng.leecode.mulitithread.multiThreadBook;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 深入理解lock 和reentrantlock
 */
public class LockDemo {
    public static void main(String[] args) {

    }

    private static class AccumulatorThread extends Thread {

        @Override
        public void run() {
            super.run();
        }
    }

    private static class Accumulator {
        private static final Lock lock = new ReentrantLock();
        private int x = 0;
        private int y = 0;

        void addX() {
            lock.lock();
            try {
                x++;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        void addY() {
            lock.lock();
            try {
                y++;
            } finally {
                lock.unlock();
            }
        }

        int getX() {
            lock.lock();
            try {
                return x;
            } finally {
                lock.unlock();
            }
        }

        int gety() {
            lock.lock();
            try {
                return y;
            } finally {
                lock.unlock();
            }
        }
    }
}
